package cn.tedu.rabbitmq.ps;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

//模拟发布订阅模式中的生产者
public class PSProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂，并设置连接信息
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1"); //设置主机
        cf.setPort(5672); //客户端连接服务端的端口号，默认是5672，可以省略
        cf.setUsername("guest");
        cf.setPassword("guest");
        //2.建立连接
        Connection nc = cf.newConnection();
        //3.在连接创建channel
        Channel cc = nc.createChannel();
        //4.定义交换机
        cc.exchangeDeclare("logs","fanout");
        //5.循环的准备消息，发送消息
        //死循环在控制台发送消息
        while (true){
            System.out.print("输入消息:");
            String msg = new Scanner(System.in).nextLine();
            if (msg.equals("quit"))
                break;
            cc.basicPublish("logs","", null,msg.getBytes());
        }
        nc.close();
    }
}
